var constant = require('../service/constant.js');

function check_require(formData) {
  for (var key in formData) {
    var cellData = formData[key];
    if (cellData.isRequired) {
      var requiredTip = cellData.requiredRule.requeiredTip !== undefined ? cellData.requiredRule.requeiredTip : cellData.title;
      if (cellData.value === undefined || cellData.value.length == 0) {
        wx.showModal({
          title: '提示',
          content: "请输入" + requiredTip,
          success: function (res) {
            if (res.confirm) {
            } else {
            }
          }
        });
        return false;
      }

      if (cellData.requiredRule) {
        if (cellData.requiredRule.length) {
          var length = Number(cellData.requiredRule.length);
          if (cellData.value.length != length) {
            wx.showModal({
              title: '提示',
              content: requiredTip + "长度必须为" + length,
              success: function (res) {
                if (res.confirm) {
                } else {
                }
              }
            });
            return false;
          }
        }

        if (cellData.requiredRule.minlength){

        }

        if (cellData.requiredRule.maxlength) {

        }
        
      }
    }
  }
  return true;
}

function POST(url, data, callback) {
  console.log(url + "\n" + JSON.stringify(data) + "\n");
  wx.request({
    url: url,
    data: data,
    method: 'post',
    header: { 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8' },
    success: function (res) {
      if (typeof cb == "function") {
        if (res.statusCode == 200) {
          cb(res.data);
        } else {
          cb(false);
        }
      }
    },
    fail: function () {
      return typeof cb == "function" && cb(false)
    }
  })
}

function register_send_sms(formData, callback) {
  POST(constant.send_sms, formData, callback);
}

module.exports = {
  check_require: check_require,
  register_send_sms: register_send_sms
}